package service

import (
	"gitee.com/gopher2011/mall/app/dao"
	"time"
)

// 首页广告管理
var SmsHomeAdvertise = smsHomeAdvertiseService{}

// 私有对象，所有的方法都绑定到这个结构体上。

type smsHomeAdvertiseService struct{}

//这个方法没有问题，详情请看 mall_test.go 中 TestSmsHomeAdvertise()
func (p *smsHomeAdvertiseService) Create(arg dao.SmsHomeAdvertiseReq) (int64, error) {
	// 将字符串时间转换成 time.Time类型时间
	startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", arg.StartTime, time.Local)
	endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", arg.EndTime, time.Local)
	u := dao.SmsHomeAdvertise{
		Name:       arg.Name,
		Types:      arg.Types,
		Pic:        arg.Pic,
		StartTime:  startTime,
		EndTime:    endTime,
		Status:     arg.Status,
		ClickCount: 0,
		OrderCount: 0,
		Url:        arg.Url,
		Note:       arg.Note,
		Sort:       arg.Sort,
	}
	return dao.SmsHomeAdvertiseDao.InsertTemplate(u)
}

// 批量删除广告
func (p *smsHomeAdvertiseService) Delete(ids []int64) (int64, error) {
	return dao.SmsHomeAdvertiseDao.DeleteTemplate(ids)
}

func (p *smsHomeAdvertiseService) UpdateStatus(id int64, status int32) (int64, error) {
	return dao.SmsHomeAdvertiseDao.UpdateStatus(id, status)
}

func (p *smsHomeAdvertiseService) Update(id int64, arg dao.SmsHomeAdvertiseReq) (int64, error) {
	// 将字符串时间转换成 time.Time类型时间
	startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", arg.StartTime, time.Local)
	endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", arg.EndTime, time.Local)
	u := dao.SmsHomeAdvertise{
		Id:         id,
		Name:       arg.Name,
		Types:      arg.Types,
		Pic:        arg.Pic,
		StartTime:  startTime,
		EndTime:    endTime,
		Status:     arg.Status,
		ClickCount: 0,
		OrderCount: 0,
		Url:        arg.Url,
		Note:       arg.Note,
		Sort:       arg.Sort,
	}
	return dao.SmsHomeAdvertiseDao.UpdateTemplate(u)
}

func (p *smsHomeAdvertiseService) GetItem(id int64) (dao.SmsHomeAdvertise, error) {
	return dao.SmsHomeAdvertiseDao.SelectTemplate(id)
}

func (p *smsHomeAdvertiseService) List(arg dao.SmsHomeAdvertiseStr) ([]dao.SmsHomeAdvertise, error) {
	start := arg.EndTime + " 00:00:00" //注意: " 00:00:00"这个字符串前面必须有一个空格
	arg.EndTime = arg.EndTime + " 23:59:59"
	startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", start, time.Local)
	endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", arg.EndTime, time.Local)
	u := dao.SmsHomeAdvertiseList{
		Name:      arg.Name,
		Types:     arg.Types,
		PageNum:   (arg.PageNum - 1) * arg.PageSize,
		PageSize:  arg.PageSize,
		StartTime: startTime,
		EndTime:   endTime,
	}
	return dao.SmsHomeAdvertiseDao.List(u)
}
